package com.gqz.equip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.TEnWzEquipEntity;
import com.gqz.equip.app.mapper.TEnWzEquipMapper;
import com.gqz.equip.entity.TEnWzCrkOrderEntity;
import com.gqz.equip.entity.TEnWzCrkOrderEquipEntity;
import com.gqz.equip.mapper.TEnWzCrkOrderEquipMapper;
import com.gqz.equip.mapper.TEnWzCrkOrderMapper;
import com.gqz.equip.mapstruct.TEnWzCrkOrderMapstruct;
import com.gqz.equip.service.ITEnWzCrkOrderEquipService;
import com.gqz.equip.service.ITEnWzCrkOrderService;
import com.gqz.equip.vo.TEnWzCrkOrderEquipVo;
import com.gqz.equip.vo.TEnWzCrkOrderListVo;
import com.gqz.equip.vo.TEnWzCrkOrderVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * 新能源-物资-出入库工单表 业务逻辑实现层ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class TEnWzCrkOrderServiceImpl extends ServiceImpl<TEnWzCrkOrderMapper, TEnWzCrkOrderEntity> implements ITEnWzCrkOrderService {

    @Qualifier("TEnWzCrkOrderMapstructImpl")
    @Autowired
    private TEnWzCrkOrderMapstruct mapstruct;

    @Resource
    private ITEnWzCrkOrderEquipService itEnWzCrkOrderEquipService;
    @Resource
    private TEnWzCrkOrderEquipMapper tEnWzCrkOrderEquipMapper;
    @Resource
    private TEnWzEquipMapper tEnWzEquipMapper;

    @Override
    public List<TEnWzCrkOrderVo> getById(String id) {
        MPJLambdaWrapper<TEnWzCrkOrderEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.eq(TEnWzCrkOrderEntity::getId, id);
        queryWrapper.selectAll(TEnWzCrkOrderEntity.class);
        List<TEnWzCrkOrderEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<TEnWzCrkOrderVo> page(RequestForm<TEnWzCrkOrderVo> requestForm) {

        MPJLambdaWrapper<TEnWzCrkOrderEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzCrkOrderEntity.class);
        queryWrapper.eq(TEnWzCrkOrderEntity::getSfsc, 0);
        queryWrapper.orderByDesc(TEnWzCrkOrderEntity::getCjTime);
        if (requestForm.getEntity().getId() != null && !"".equals(requestForm.getEntity().getId())) {
            queryWrapper.like(TEnWzCrkOrderEntity::getId, requestForm.getEntity().getId());
        }
        if (requestForm.getEntity().getDjshzt() != null && !"".equals(requestForm.getEntity().getDjshzt())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getDjshzt, requestForm.getEntity().getDjshzt());
        }
        if (requestForm.getEntity().getCkbm() != null && !"".equals(requestForm.getEntity().getCkbm())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getCkbm, requestForm.getEntity().getCkbm());
        }
        if (requestForm.getEntity().getDjrq() != null && !"".equals(requestForm.getEntity().getDjrq())) {
            queryWrapper.ge(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq());
        }
        if (requestForm.getEntity().getDjrq_to() != null && !"".equals(requestForm.getEntity().getDjrq_to())) {
            queryWrapper.le(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq_to());
        }
        if (requestForm.getEntity().getType() != null && !"".equals(requestForm.getEntity().getType())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getType, requestForm.getEntity().getType());
        }
        if (requestForm.getEntity().getSsdwbm() != null && !"".equals(requestForm.getEntity().getSsdwbm())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getSsdwbm, requestForm.getEntity().getSsdwbm());
        }
        Page<TEnWzCrkOrderEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnWzCrkOrderEntity> iPage = baseMapper.selectJoinPage(page, TEnWzCrkOrderEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Page<TEnWzCrkOrderVo> shPage(RequestForm<TEnWzCrkOrderVo> requestForm) {

        MPJLambdaWrapper<TEnWzCrkOrderEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzCrkOrderEntity.class);
        queryWrapper.eq(TEnWzCrkOrderEntity::getSfsc, 0);
        queryWrapper.notIn(TEnWzCrkOrderEntity::getDjshzt, 0);
        queryWrapper.orderByDesc(TEnWzCrkOrderEntity::getDjshzt);
        queryWrapper.orderByDesc(TEnWzCrkOrderEntity::getCjTime);
        if (requestForm.getEntity().getId() != null && !"".equals(requestForm.getEntity().getId())) {
            queryWrapper.like(TEnWzCrkOrderEntity::getId, requestForm.getEntity().getId());
        }
        if (requestForm.getEntity().getDjshzt() != null && !"".equals(requestForm.getEntity().getDjshzt())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getDjshzt, requestForm.getEntity().getDjshzt());
        }
        if (requestForm.getEntity().getCkbm() != null && !"".equals(requestForm.getEntity().getCkbm())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getCkbm, requestForm.getEntity().getCkbm());
        }
        if (requestForm.getEntity().getDjrq() != null && !"".equals(requestForm.getEntity().getDjrq())) {
            queryWrapper.ge(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq());
        }
        if (requestForm.getEntity().getDjrq_to() != null && !"".equals(requestForm.getEntity().getDjrq_to())) {
            queryWrapper.le(TEnWzCrkOrderEntity::getDjrq, requestForm.getEntity().getDjrq_to());
        }
        if (requestForm.getEntity().getType() != null && !"".equals(requestForm.getEntity().getType())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getType, requestForm.getEntity().getType());
        }
        if (requestForm.getEntity().getSsdwbm() != null && !"".equals(requestForm.getEntity().getSsdwbm())) {
            queryWrapper.eq(TEnWzCrkOrderEntity::getSsdwbm, requestForm.getEntity().getSsdwbm());
        }
        // System.out.println("----------------"+Timestamp.valueOf("2022-01-01 00:00:00"));
        Page<TEnWzCrkOrderEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<TEnWzCrkOrderEntity> iPage = baseMapper.selectJoinPage(page, TEnWzCrkOrderEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<TEnWzCrkOrderVo> requestForm) {
        TEnWzCrkOrderEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<TEnWzCrkOrderEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(TEnWzCrkOrderEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(TEnWzCrkOrderListVo vos) {
        // List<TEnWzCrkOrderEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        TEnWzCrkOrderEntity crkOrderEntity = new TEnWzCrkOrderEntity();
        crkOrderEntity.setId(vos.getId());
        crkOrderEntity.setDjrq(vos.getDjrq());
        crkOrderEntity.setCkbm(vos.getCkbm());
        crkOrderEntity.setCkmc(vos.getCkmc());
        crkOrderEntity.setSsdwbm(vos.getSsdwbm());
        crkOrderEntity.setSsdwmc(vos.getSsdwmc());
        crkOrderEntity.setCjr(vos.getCjr());
        crkOrderEntity.setCjrbm(vos.getCjrbm());
        crkOrderEntity.setCjTime(vos.getCjTime());
        crkOrderEntity.setDjshzt("0");
        crkOrderEntity.setDjshztmc("创建");
        //0-入库  1-出库
        crkOrderEntity.setType(vos.getType());
        List<TEnWzCrkOrderEquipVo> equipEntityList = vos.getEquipList();
        crkOrderEntity.setSl(equipEntityList.size() + "");
        for (int i = 0, size = equipEntityList.size(); i < size; i++) {
            //先将入库或出库单的设备进行保存
            TEnWzCrkOrderEquipEntity equipEntity = new TEnWzCrkOrderEquipEntity();
            equipEntity.setDjbm(vos.getId());
            equipEntity.setSbbm(equipEntityList.get(i).getSbbm());
            tEnWzCrkOrderEquipMapper.insert(equipEntity);
            //将相关设备状态改为 申请入库中 或 申请出库中  且保留原有设备状态 以防止还原操作
            TEnWzEquipEntity tEnWzEquipEntity = new TEnWzEquipEntity();
            tEnWzEquipEntity.setSbbm(equipEntityList.get(i).getSbbm());
            tEnWzEquipEntity.setSbztOld(equipEntityList.get(i).getSbztbm());
            tEnWzEquipEntity.setSbztOldmc(equipEntityList.get(i).getSbztOldmc());
            if (vos.getType() == "0" || "0".equals(vos.getType())) {
                tEnWzEquipEntity.setSbztbm("10");
                tEnWzEquipEntity.setSbztmc("申请入库中");
            } else if (vos.getType() == "1" || "1".equals(vos.getType())) {
                tEnWzEquipEntity.setSbztbm("30");
                tEnWzEquipEntity.setSbztmc("申请出库中");
            }
            tEnWzEquipMapper.updateById(tEnWzEquipEntity);
        }
        result += baseMapper.insert(crkOrderEntity);
        return result;
    }

    @Override
    public int update(TEnWzCrkOrderListVo vos) {
        int result = 0;
        //第一步 将原有关联的设备全部删除，且还原设备状态
        List<TEnWzCrkOrderEquipVo> tEnWzCrkOrderEquipVos = itEnWzCrkOrderEquipService.getByDjbh(vos.getId());
        for (int i = 0; i < tEnWzCrkOrderEquipVos.size(); i++) {
            TEnWzEquipEntity tEnWzEquipEntity_v1 = new TEnWzEquipEntity();
            tEnWzEquipEntity_v1.setSbztbm(tEnWzCrkOrderEquipVos.get(i).getSbztOld());
            tEnWzEquipEntity_v1.setSbztmc(tEnWzCrkOrderEquipVos.get(i).getSbztOldmc());
            tEnWzEquipEntity_v1.setSbbm(tEnWzCrkOrderEquipVos.get(i).getSbbm());
            tEnWzEquipMapper.updateById(tEnWzEquipEntity_v1);
            TEnWzCrkOrderEquipEntity equipEntity_v1 = new TEnWzCrkOrderEquipEntity();
            equipEntity_v1.setDjbm(tEnWzCrkOrderEquipVos.get(i).getDjbm());
            equipEntity_v1.setId(tEnWzCrkOrderEquipVos.get(i).getId());
            equipEntity_v1.setSfsc("1");
            tEnWzCrkOrderEquipMapper.updateById(equipEntity_v1);
        }
        //第二步 1.修改单据信息 2.入库单据设备  3.将设备的状态更改为入库申请中和出库申请中
        TEnWzCrkOrderEntity crkOrderEntity = new TEnWzCrkOrderEntity();
        crkOrderEntity.setId(vos.getId());
        crkOrderEntity.setDjrq(vos.getDjrq());
        crkOrderEntity.setCkbm(vos.getCkbm());
        crkOrderEntity.setCkmc(vos.getCkmc());
        crkOrderEntity.setSsdwbm(vos.getSsdwbm());
        crkOrderEntity.setSsdwmc(vos.getSsdwmc());
        crkOrderEntity.setCjr(vos.getCjr());
        crkOrderEntity.setCjTime(vos.getCjTime());
        //0-入库  1-出库
        crkOrderEntity.setType(vos.getType());
        List<TEnWzCrkOrderEquipVo> equipEntityList = vos.getEquipList();
        crkOrderEntity.setSl(equipEntityList.size() + "");
        for (int i = 0, size = equipEntityList.size(); i < size; i++) {
            //先将入库或出库单的设备进行保存
            TEnWzCrkOrderEquipEntity equipEntity = new TEnWzCrkOrderEquipEntity();
            equipEntity.setDjbm(vos.getId());
            equipEntity.setSbbm(equipEntityList.get(i).getSbbm());
            tEnWzCrkOrderEquipMapper.insert(equipEntity);
            //将相关设备状态改为 申请入库中 或 申请出库中  且保留原有设备状态 以防止还原操作
            TEnWzEquipEntity tEnWzEquipEntity = new TEnWzEquipEntity();
            tEnWzEquipEntity.setSbbm(equipEntityList.get(i).getSbbm());
            tEnWzEquipEntity.setSbztOld(equipEntityList.get(i).getSbztbm());
            tEnWzEquipEntity.setSbztOldmc(equipEntityList.get(i).getSbztOldmc());
            if (vos.getType() == "0" || "0".equals(vos.getType())) {
                tEnWzEquipEntity.setSbztbm("10");
                tEnWzEquipEntity.setSbztmc("申请入库中");
            } else if (vos.getType() == "1" || "1".equals(vos.getType())) {
                tEnWzEquipEntity.setSbztbm("30");
                tEnWzEquipEntity.setSbztmc("申请出库中");
            }
            tEnWzEquipMapper.updateById(tEnWzEquipEntity);
        }
        result += baseMapper.updateById(crkOrderEntity);
        return result;
    }

    @Override
    public String getDjbh() {
        String timeStr2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        Random random = new Random();
        int num = random.nextInt(100);
        return timeStr2 + "" + num;
    }

    @Override
    public int deleteByIds(String[] ids) {
        int result = 0;
        //第一步 原单据软删
        // String[] idList=ids.split(",");
        String[] idList = ids;
        //第二步 设备状态还原
        for (int i = 0; i < idList.length; i++) {
            TEnWzCrkOrderEntity crkOrderEntity = new TEnWzCrkOrderEntity();
            crkOrderEntity.setId(idList[i]);
            crkOrderEntity.setSfsc("1");
            List<TEnWzCrkOrderEquipVo> tEnWzCrkOrderEquipVos = itEnWzCrkOrderEquipService.getByDjbh(idList[i]);
            for (int j = 0; j < tEnWzCrkOrderEquipVos.size(); j++) {
                TEnWzEquipEntity tEnWzEquipEntity_v1 = new TEnWzEquipEntity();
                tEnWzEquipEntity_v1.setSbztbm(tEnWzCrkOrderEquipVos.get(j).getSbztOld());
                tEnWzEquipEntity_v1.setSbztmc(tEnWzCrkOrderEquipVos.get(j).getSbztOldmc());
                tEnWzEquipEntity_v1.setSbbm(tEnWzCrkOrderEquipVos.get(j).getSbbm());
                tEnWzEquipMapper.updateById(tEnWzEquipEntity_v1);
            }
            result += baseMapper.updateById(crkOrderEntity);
        }
        return result;
    }

    @Override
    public int submitShByIds(String[] ids) {

        int result = 0;
        String[] idList = ids;
        for (int i = 0; i < idList.length; i++) {
            TEnWzCrkOrderEntity crkOrderEntity = new TEnWzCrkOrderEntity();
            crkOrderEntity.setId(idList[i]);
            crkOrderEntity.setDjshzt("10");
            crkOrderEntity.setDjshztmc("待审核");
            result += baseMapper.updateById(crkOrderEntity);
        }
        return result;
    }

    @Override
    public int shDj(TEnWzCrkOrderVo vos) {
        int result = 0;
        //判断审核是否通过  20-退回  100-归档
        if ("20".equals(vos.getDjshzt())) {
            TEnWzCrkOrderEntity crkOrderEntity = new TEnWzCrkOrderEntity();
            crkOrderEntity.setId(vos.getId());
            crkOrderEntity.setDjshzt("20");
            crkOrderEntity.setDjshztmc("退回");
            crkOrderEntity.setShyj(vos.getShyj());
            crkOrderEntity.setShPerson(vos.getShPerson());
            crkOrderEntity.setShTime(vos.getShTime());

            crkOrderEntity.setShjlbm(vos.getShjlbm());
            crkOrderEntity.setShjlmc(vos.getShjlmc());
            result += baseMapper.updateById(crkOrderEntity);
        } else if ("100".equals(vos.getDjshzt())) {
            TEnWzCrkOrderEntity crkOrderEntity = new TEnWzCrkOrderEntity();
            crkOrderEntity.setId(vos.getId());
            crkOrderEntity.setDjshzt("100");
            crkOrderEntity.setDjshztmc("归档");
            crkOrderEntity.setShyj(vos.getShyj());
            crkOrderEntity.setShPerson(vos.getShPerson());
            crkOrderEntity.setShTime(vos.getShTime());

            crkOrderEntity.setShjlbm(vos.getShjlbm());
            crkOrderEntity.setShjlmc(vos.getShjlmc());
            result += baseMapper.updateById(crkOrderEntity);
            //判断是入库还是出库单
            String ztbm = "";
            String ztmc = "";
            if ("0".equals(vos.getType())) {
                ztbm = "20";
                ztmc = "已入库";
            } else if ("1".equals(vos.getType())) {
                ztbm = "40";
                ztmc = "已出库";
            }
            List<TEnWzCrkOrderEquipVo> tEnWzCrkOrderEquipVos = itEnWzCrkOrderEquipService.getByDjbh(vos.getId());
            for (int j = 0; j < tEnWzCrkOrderEquipVos.size(); j++) {
                TEnWzEquipEntity tEnWzEquipEntity = new TEnWzEquipEntity();
                tEnWzEquipEntity.setSbztbm(ztbm);
                tEnWzEquipEntity.setSbztmc(ztmc);
                tEnWzEquipEntity.setSbztOld(ztbm);
                tEnWzEquipEntity.setSbztOldmc(ztmc);
                tEnWzEquipEntity.setSbbm(tEnWzCrkOrderEquipVos.get(j).getSbbm());
                tEnWzEquipMapper.updateById(tEnWzEquipEntity);
            }
            result += baseMapper.updateById(crkOrderEntity);
        }
        return result;
    }


}
